﻿<?php
/**
 * 注册控制
 */
//注册会员名
$register_user=!empty($_POST['user_name'])?$fun->checkInput(trim($_POST['user_name'])):"";
//输入的密码
$register_pwd_input=!empty($_POST['password'])?trim($_POST['password']):"";
//确认密码
$register_pwd_confirm_input=!empty($_POST['password_confirm'])?trim($_POST['password_confirm']):"";
//注册邮箱
$register_email=!empty($_POST['email'])?$fun->checkInput(trim($_POST['email'])):"";
//验证码
$register_icode=!empty($_POST['captcha'])?trim($_POST['captcha']):"";
//加密密码
$register_pwd=md5($register_pwd_confirm_input.$pwdconstant);

if(!empty($userInfo['uid'])){
	$fun->msg("您已经登录,正在进入首页...",$list_url['index'],2,$title="注册提示",$back_color="f78a12");
}else if(($register_user== "")||(empty($register_pwd_input))||($register_pwd_confirm_input=="")||(empty($register_email))||($register_icode == "")){
	$fun->msg("请您认真填写注册信息",$list_url['register'],2,$title="注册提示",$back_color="f78a12");
}else{
	
	if($register_pwd_input<>$register_pwd_confirm_input){
		$fun->msg("您两次输入的密码不一致",$list_url['register'],2,$title="注册提示",$back_color="f78a12");
	}elseif(strcasecmp($register_icode,@$_SESSION['randcode'])<>0){
		$fun->msg("您输入的验证码错误",$list_url['register'],2,$title="注册提示",$back_color="f78a12");
	}else{
		$check_user=$db->get_one("SELECT `uid`, `status`, `uname` FROM `".$tbprefix."user` WHERE  `uname`='".$register_user."'  AND `is_valid`='1' AND 1");
	
		if($check_user['uid']){
			$fun->msg("会员名已经注册",$list_url['register'],2,$title="注册提示",$back_color="f78a12");
			$db->close();
		}else{
			$check_email=$db->get_one("SELECT `uid`, `status`, `uname` FROM `".$tbprefix."user` WHERE `email`='".$register_email."' AND `is_valid`='1' AND 1");
			if($check_email['uid']){
				$fun->msg("邮箱已经注册",$list_url['register'],2,$title="注册提示",$back_color="f78a12");
				$db->close();
			}else{
				/**
				 * 建立会话变量,储存注册管理员id
				 */
				/**
				 * 成功注册，更新注册时间与登录次数
				*/
				$dataArray['uname']=$register_user;
				$dataArray['password']=$register_pwd;
				$dataArray['email']=$register_email;
				//注册时间
				$dataArray['addtime']=$m_now_time;
				//登录时间
				$dataArray['logintime']=$m_now_time;
				//最后登录时间
				$dataArray['lastlogintime']=$m_now_time;
				//登录次数
				$dataArray['login_num']=1;
				//有效数据
				$dataArray['is_valid']=1;
				//激活状态
				$dataArray['status']=1;
				//注册IP
				$dataArray['modify_ip']=$fun->get_web_ip();
				
				if($db->insert($table="`".$tbprefix."user`",$dataArray))
				{
					$insert_uid=$db->insert_id();
					//存储登录的基本信息
					@$_SESSION['web_user_uid']=$fun->authcode($insert_uid,'ENCODE', $pwdconstant,$expiry=0);
					
					
					/* shopnc 模拟注册 开始 */
					
					$shopnc_url = SHOPNC_DOMAIN;
					$login_url_s = $shopnc_url .'index.php?act=login&op=usersave';
					
					
					$post_fields = array();
					$post_fields['user_name'] = $register_user;
					$post_fields['password'] = $register_pwd_input;
					$post_fields['email'] = $register_email;
					$post_fields['agree'] = 1;
					
					$ch = curl_init($login_url_s);
					curl_setopt($ch, CURLOPT_HEADER, 0);
					curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
					curl_setopt($ch, CURLOPT_POST, 1);
					curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
					curl_exec($ch);
					curl_close($ch);
					
					
					/* shopnc 模拟注册 开始  ok */
					 	
					 	 	
					/* discuz 模拟注册 开始 */
					
					$discuz_url = DISCUZ_DOMAIN;
					$login_url_d = $discuz_url .'member.php?mod=xingxunwang';
					
					
					$post_fields = array();
					$post_fields['username'] = $register_user;
					$post_fields['password'] = $register_pwd_input;
					$post_fields['password2'] = $register_pwd_input;
					$post_fields['email'] = $register_email;
					
					$post_fields['regsubmit'] = 'yes';
					$post_fields['referer'] = '';
					$post_fields['activationauth'] = '';
					
					$ch = curl_init($login_url_d);
					curl_setopt($ch, CURLOPT_HEADER, 0);
					curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
					curl_setopt($ch, CURLOPT_POST, 1);
					curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
					$rs = curl_exec($ch);
					curl_close($ch);
					
					//echo $rs; //8c40ed10d774f9265df25d5c3e070f84     ed2b1f468c5f915f3f1cf75d7068baae
					//exit;
					/* discuz 模拟注册 开始 */
					
					//同步登陆
					
					/* shopnc login */
					
					$shoplogin = SHOPNC_DOMAIN."index.php?act=login&user_name={$register_user}&password={$register_pwd_input}&form_submit=ok";
					echo '<iframe height=0 width=0 style="display:none;" src="'.$shoplogin.'"></iframe>';
					
					/* shopnc login ok*/
					/* discuz login */
					
					$discuzlogin = DISCUZ_DOMAIN."member.php?mod=logging&action=login&loginsubmit=yes&handlekey=login&username={$register_user}&password={$register_pwd_input}";
					echo '<iframe height=0 width=0 style="display:none;" src="'.$discuzlogin.'"></iframe>';
					
					/* discuz login */
						
					$backurl = $fun->back_to_which_from($url_from);
					
					
					$fun->msg("欢迎  ".$register_user." 加入我们的大家庭",$backurl,1,$title="注册提示",$back_color="f78a12");
					
					$db->close();
				}
				else
				{
					$fun->msg("注册失败,请您重新注册",$list_url['register'],2,$title="注册提示",$back_color="f78a12");
				}
			}

		}
	}
}
?>